<?php

namespace App\Http\Controllers\Adm\Punch;

use App\Models\punch_activity;
use App\Models\user_punch_record;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

class ActivityController extends Controller
{

    /**
     * 订单流水
     */
    public function punchList(Request $request)
    {
        $ktime = !empty($request->input('ktime')) ? ($request->input('ktime')) : 0;
        $gtime = !empty($request->input('gtime')) ? ($request->input('gtime')) : 0;
        $channel_account = $request->input('channel_account','');
        $status = $request->input('status','');
        if($ktime==$gtime &&  $ktime){
            $ktime = date("Y-m-d H:i:s",strtotime($ktime));
            $gtime = date("Y-m-d H:i:s",(strtotime($ktime)+86400));
        }
        $where = [];
        if($channel_account) $where['channel_account'] = $channel_account;
        if($status) $where['status'] = $status;
        //$res = money_record::getList($where,$ktime,$gtime);
        $res = punch_activity::where($where)->paginate(5);
        //var_dump($res);exit;
        if($res){
            foreach ($res as &$v){
                $join_punch = user_punch_record::leftJoin('user','user_punch_record.user_id','=','user.openid')
                    ->where(['user_punch_record.punch_ac_id'=>$v['id'],'user_punch_record.is_valid'=>1])->groupBy(['user_punch_record.user_id'])
                    ->get(['avatarUrl']);
                $v['join_num'] = $join_punch?count($join_punch):0;
            }
        }
        return view('admin.punch.activity',[
            'list'=>$res,
            'channel_account'=>$channel_account,
            'status'=>$status,
            'ktime'=>$ktime,
            'gtime'=>$gtime,
        ]);
    }


    public function hot(Request $request){
        $punch_id = $request->input('punch_id');
        $value = $request->input('value');
        $res = punch_activity::where(['id'=>$punch_id])->update(['hot'=>$value]);
        if($res){
            fun_respon(1, '操作成功！');
        }else{
            fun_respon(0, '操作失败！');

        }
    }
}
